package controllerlayer;

import java.util.ArrayList;

import modellayer.KeyPerson;
import dblayer.KeyPersonDB;

/**
 * The KeyPersonCtrl class to handle KeyPerson information
 * @author Allan Eriksen, Andreas Rye, Casper Madsen, Christian Stidsborg, Stefan Bols
 */

public class KeyPersonCtrl
{
	private KeyPersonDB keyPersonDB;
	/**
	 * @param keyPersonDB
	 */
	
	/**
	 * Constructor for the keyPersonCtrl class
	 */
	public KeyPersonCtrl()
	{
		keyPersonDB = new KeyPersonDB();
	}
	
	/**
	 * Method for searching for a keyPerson using keyPersonId
	 * @param keyPersonId
	 * @return result
	 */
	public KeyPerson findKeyPersonById(int keyPersonId)
	{
		KeyPerson result = keyPersonDB.findKeyPersonById(keyPersonId);
		
		return result;
	}
	
	/**
	 * Method for return an ArrayList with all keyPersons in it
	 * @return keyPersonDB.listAllKeyPersons
	 */
	public ArrayList<KeyPerson> listOfAllKeyPerson()
	{
		return keyPersonDB.listAllKeyPersons();
	}
	
	/**
	 * Method for searching for a keyPerson using a name
	 * @param name
	 * @return result
	 */
	public KeyPerson findKeyPersonByName(String name)
	{
		int i = 0;
		boolean found = false;
		KeyPerson result = null;
		ArrayList<KeyPerson> array = listOfAllKeyPerson();
		int arraySize = array.size();
		
		while (i<arraySize && !found)
		{
			if (array.get(i).getName().equals(name))
			{
				result = array.get(i);
				found = true;
			}
			i++;
		}
		
		return result;
	}
	
	public boolean deleteKeyPersonById(int id) throws Exception {
		return keyPersonDB.deleteKeyPerson(id);
	}
}
